<template>
  <div class="app-container">
    <el-row :gutter="20">
      <!-- 部门 -->
      <el-col :span="5" :xs="24">
        <div class="head-container el_tree_ground">
          <el-input v-model="deptName" placeholder="请输入部门名称" clearable size="small" prefix-icon="el-icon-search"
            style="margin-bottom: 20px" />
        </div>
        <div class="head-container" style="width: 100%;height:600px;overflow:hidden;">
          <el-tree style="width: 106%; height:100%;overflow:auto;overflow-x:hidden;" node-key="id"
            :default-expanded-keys="[1]" :data="deptOptions" :props="defaultProps" :expand-on-click-node="false"
            highlight-current :filter-node-method="filterNode" ref="tree" @node-click="handleNodeClick">
            <!--过长处理，鼠标悬浮文字弹框显示全部内容-->
            <span slot-scope="{ node }">
              <el-tooltip class="item" effect="light" :content="node.label" placement="top-start"
                v-if="node.label.length>12">
                <span> {{ node.label | ellipsis(12) }} </span>
              </el-tooltip>
              <span v-if="node.label.length<=12">{{node.label}}</span>
            </span>
          </el-tree>
        </div>
      </el-col>

      <el-col :span="19" :xs="24" style="margin-top: 0px;">

        <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch">
          <el-form-item label="日期" prop="holidayDate">
            <el-date-picker clearable size="small" style="max-width: 200px;" v-model="queryParams.holidayDate"
              type="date" placeholder="选择选择日期">
            </el-date-picker>
          </el-form-item>
          <el-form-item label="节日名称" prop="holidayName">
            <el-input v-model="queryParams.holidayName" placeholder="请输入节日名称" clearable size="small"
              @keyup.enter.native="handleQuery" />
          </el-form-item>
          <el-form-item>
            <el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-form>
        <el-row>
          <span style="font-size: 14px;">当前实验室:</span><span
            style="margin-left: 10px;font-size: 16px;">{{currentLabName}}</span>
        </el-row>
        <el-table v-loading="loading" :data="labReservationList" @selection-change="handleSelectionChange">
          <el-table-column label="日期" align="center" prop="holidayDate" />
          <el-table-column label="节日名称" align="center" prop="holidayName" width="80" />
          <el-table-column label="周次" align="center" prop="weekNum" />
		  <el-table-column label="工作日" align="center" prop="holidayFlag" :formatter="flagOptFormat" width="60" />
          <el-table-column label="1" align="center" prop="sectionOne" width="35">
            <template slot-scope="scope">
              <i v-if="scope.row.sectionOne =='0'" style="color: green;" class="el-icon-circle-check"></i>
              <i v-else style="color: red;" class="el-icon-circle-close"></i>
            </template>
          </el-table-column>
          <el-table-column label="2" align="center" prop="sectionTwo" width="35">
            <template slot-scope="scope">
              <i v-if="scope.row.sectionTwo =='0'" style="color: green;" class="el-icon-circle-check"></i>
              <i v-else style="color: red;" class="el-icon-circle-close"></i>
            </template>
          </el-table-column>
          <el-table-column label="3" align="center" prop="sectionThree" width="35">
            <template slot-scope="scope">
              <i v-if="scope.row.sectionThree =='0'" style="color: green;" class="el-icon-circle-check"></i>
              <i v-else style="color: red;" class="el-icon-circle-close"></i>
            </template>
          </el-table-column>
          <el-table-column label="4" align="center" prop="sectionFour" width="35">
            <template slot-scope="scope">
              <i v-if="scope.row.sectionFour =='0'" style="color: green;" class="el-icon-circle-check"></i>
              <i v-else style="color: red;" class="el-icon-circle-close"></i>
            </template>
          </el-table-column>
          <el-table-column label="5" align="center" prop="sectionFive" width="35">
            <template slot-scope="scope">
              <i v-if="scope.row.sectionFive =='0'" style="color: green;" class="el-icon-circle-check"></i>
              <i v-else style="color: red;" class="el-icon-circle-close"></i>
            </template>
          </el-table-column>
          <el-table-column label="6" align="center" prop="sectionSix" width="35">
            <template slot-scope="scope">
              <i v-if="scope.row.sectionSix =='0'" style="color: green;" class="el-icon-circle-check"></i>
              <i v-else style="color: red;" class="el-icon-circle-close"></i>
            </template>
          </el-table-column>
          <el-table-column label="7" align="center" prop="sectionSeven" width="35">
            <template slot-scope="scope">
              <i v-if="scope.row.sectionSeven =='0'" style="color: green;" class="el-icon-circle-check"></i>
              <i v-else style="color: red;" class="el-icon-circle-close"></i>
            </template>
          </el-table-column>
          <el-table-column label="8" align="center" prop="sectionEight" width="35">
            <template slot-scope="scope">
              <i v-if="scope.row.sectionEight =='0'" style="color: green;" class="el-icon-circle-check"></i>
              <i v-else style="color: red;" class="el-icon-circle-close"></i>
            </template>
          </el-table-column>
          <el-table-column label="9" align="center" prop="sectionNine" width="35">
            <template slot-scope="scope">
              <i v-if="scope.row.sectionNine =='0'" style="color: green;" class="el-icon-circle-check"></i>
              <i v-else style="color: red;" class="el-icon-circle-close"></i>
            </template>
          </el-table-column>
          <el-table-column label="10" align="center" prop="sectionTen" width="35">
            <template slot-scope="scope">
              <i v-if="scope.row.sectionTen =='0'" style="color: green;" class="el-icon-circle-check"></i>
              <i v-else style="color: red;" class="el-icon-circle-close"></i>
            </template>
          </el-table-column>
          <el-table-column label="11" align="center" prop="sectionEleven" width="35">
            <template slot-scope="scope">
              <i v-if="scope.row.sectionEleven =='0'" style="color: green;" class="el-icon-circle-check"></i>
              <i v-else style="color: red;" class="el-icon-circle-close"></i>
            </template>
          </el-table-column>
          <el-table-column label="12" align="center" prop="sectionTwelve" width="35">
            <template slot-scope="scope">
              <i v-if="scope.row.sectionTwelve =='0'" style="color: green;" class="el-icon-circle-check"></i>
              <i v-else style="color: red;" class="el-icon-circle-close"></i>
            </template>
          </el-table-column>
          <el-table-column label="操作" align="center" width="60" v-if="isPermit('labdata:labReservation:edit')">
            <template slot-scope="scope">
              <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)">修改</el-button>
            </template>
          </el-table-column>
        </el-table>

        <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
          @pagination="getList" />

        <!-- 添加或修改实验室是否可预约对话框 -->
        <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body>
          <el-form ref="form" :model="form" :rules="rules" label-width="80px" :inline="true" size="mini">
            <el-form-item label="日历年份" prop="year" v-if="form.dayId==null">
              <el-date-picker clearable size="small" style="max-width: 200px;" v-model="form.year" type="year"
                value-format="yyyy" placeholder="选择生成日历的年份" class="input_w">
              </el-date-picker>
            </el-form-item>
            <!-- <el-form-item label="实验室" prop="year" v-if="form.dayId==null">
              <el-select v-model="form.classRoomId" placeholder="请选择实验室">
                <el-option v-for="dict in labOrgList" :key="dict.orgId" :label="dict.orgName" :value="dict.orgId">
                </el-option>
              </el-select>
            </el-form-item> -->
            <el-form-item label="实验室" prop="year" v-if="form.dayId">
              <el-select v-model="form.classRoomId" placeholder="请选择实验室" disabled>
                <el-option v-for="dict in labOrgList" :key="dict.orgId" :label="dict.orgName" :value="dict.orgId">
                </el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="节日名称" prop="holidayName" v-if="form.dayId">
              <el-input v-model="form.holidayName" placeholder="请输入节日名称" class="input_w" />
            </el-form-item>
            <el-form-item label="工作日" prop="holidayFlag" v-if="form.dayId">
              <el-select v-model="form.holidayFlag" placeholder="请选择标识" @change="holidayFlagChange">
                <el-option v-for="dict in holidayFlagOptions" :key="dict.dictValue" :label="dict.dictLabel"
                  :value="dict.dictValue"></el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="第一节" prop="sectionOne" v-if="form.dayId">
              <el-select v-model="form.sectionOne" placeholder="请选择第一节">
                <el-option v-for="dict in sectionOneOptions" :key="dict.dictValue" :label="dict.dictLabel"
                  :value="parseInt(dict.dictValue)"></el-option>
              </el-select>
            </el-form-item>

            <el-form-item label="第二节" prop="sectionTwo" v-if="form.dayId">
              <el-select v-model="form.sectionTwo" placeholder="请选择第二节">
                <el-option v-for="dict in sectionOneOptions" :key="dict.dictValue" :label="dict.dictLabel"
                  :value="parseInt(dict.dictValue)"></el-option>
              </el-select>
            </el-form-item>

            <el-form-item label="第三节" prop="sectionThree" v-if="form.dayId">
              <el-select v-model="form.sectionThree" placeholder="请选择第三节">
                <el-option v-for="dict in sectionOneOptions" :key="dict.dictValue" :label="dict.dictLabel"
                  :value="parseInt(dict.dictValue)"></el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="第四节" prop="sectionFour" v-if="form.dayId">
              <el-select v-model="form.sectionFour" placeholder="请选择第四节">
                <el-option v-for="dict in sectionOneOptions" :key="dict.dictValue" :label="dict.dictLabel"
                  :value="parseInt(dict.dictValue)"></el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="第五节" prop="sectionFive" v-if="form.dayId">
              <el-select v-model="form.sectionFive" placeholder="请选择第五节">
                <el-option v-for="dict in sectionOneOptions" :key="dict.dictValue" :label="dict.dictLabel"
                  :value="parseInt(dict.dictValue)"></el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="第六节" prop="sectionSix" v-if="form.dayId">
              <el-select v-model="form.sectionSix" placeholder="请选择第六节">
                <el-option v-for="dict in sectionOneOptions" :key="dict.dictValue" :label="dict.dictLabel"
                  :value="parseInt(dict.dictValue)"></el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="第七节" prop="sectionSeven" v-if="form.dayId">
              <el-select v-model="form.sectionSeven" placeholder="请选择第七节">
                <el-option v-for="dict in sectionOneOptions" :key="dict.dictValue" :label="dict.dictLabel"
                  :value="parseInt(dict.dictValue)"></el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="第八节" prop="sectionEight" v-if="form.dayId">
              <el-select v-model="form.sectionEight" placeholder="请选择第八节">
                <el-option v-for="dict in sectionOneOptions" :key="dict.dictValue" :label="dict.dictLabel"
                  :value="parseInt(dict.dictValue)"></el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="第九节" prop="sectionNine" v-if="form.dayId">
              <el-select v-model="form.sectionNine" placeholder="请选择第九节">
                <el-option v-for="dict in sectionOneOptions" :key="dict.dictValue" :label="dict.dictLabel"
                  :value="parseInt(dict.dictValue)"></el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="第十节" prop="sectionTen" v-if="form.dayId">
              <el-select v-model="form.sectionTen" placeholder="请选择第十节">
                <el-option v-for="dict in sectionOneOptions" :key="dict.dictValue" :label="dict.dictLabel"
                  :value="parseInt(dict.dictValue)"></el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="第十一节" prop="sectionEleven" v-if="form.dayId">
              <el-select v-model="form.sectionEleven" placeholder="请选择第十一节">
                <el-option v-for="dict in sectionOneOptions" :key="dict.dictValue" :label="dict.dictLabel"
                  :value="parseInt(dict.dictValue)"></el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="第十二节" prop="sectionTwelve" v-if="form.dayId">
              <el-select v-model="form.sectionTwelve" placeholder="请选择第十二节">
                <el-option v-for="dict in sectionOneOptions" :key="dict.dictValue" :label="dict.dictLabel"
                  :value="parseInt(dict.dictValue)"></el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="备注" prop="remark" v-if="form.dayId">
              <el-input v-model="form.remark" placeholder="请输入备注" />
            </el-form-item>
          </el-form>
          <div slot="footer" class="dialog-footer">
            <el-button type="primary" @click="submitForm">确 定</el-button>
            <el-button @click="cancel">取 消</el-button>
          </div>
        </el-dialog>
      </el-col>
    </el-row>
  </div>
</template>

<script>
  import { listLabReservation, getLabReservation, delLabReservation, addLabReservation, updateLabReservation, exportLabReservation } from "@/api/labdata/labReservation";
  import { treeselect, getTreeLabel } from "@/api/labdata/orgnization";
  import Treeselect from "@riophae/vue-treeselect";
  import "@riophae/vue-treeselect/dist/vue-treeselect.css";
  import { getOrgByFunctionOpt, listSecondOrgLab } from "@/api/labdata/orgnization";

  export default {
    name: "LabReservation",
    data() {
      return {
        addDisabled:false,
        // 机构id
        orgnizationId: null,
        orgnizationName: null,
        //实验室列表
        labOrgList: [],
        // 部门名称
        deptName: undefined,
        currentLabName: '',
        defaultProps: {
          children: "children",
          label: "label"
        },
        // 部门树选项
        deptOptions: [],
        isBack: true,
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 实验室是否可预约表格数据
        labReservationList: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        holidayFlagOptions: [],
        // 第一节字典
        sectionOneOptions: [],
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          holidayDate: this.parseTime(new Date(), '{y}-{m}-{d}'),
          holidayName: null,
          holidayFlag: null,
          classRoomId: 1,
        },
        // 表单参数
        form: {},
        // 表单校验
        rules: {
          holidayDate: [
            { required: true, message: "日期不能为空", trigger: "blur" }
          ],
          holidayFlag: [
            { required: true, message: "节日标识(0-节假日 1-工作日)不能为空", trigger: "blur" }
          ],
        }
      };
    },
    watch: {
      // 根据名称筛选部门树
      deptName(val) {
        this.$refs.tree.filter(val);
      }
    },
    created() {
      this.getList();
      this.getDicts("isOrNotOpt").then(response => {
        this.holidayFlagOptions = response.data;
      });
      this.getDicts("sys_reservation").then(response => {
        this.sectionOneOptions = response.data;
      });
      getOrgByFunctionOpt(4).then(response => {
        this.labOrgList = response.data;
        console.log(this.labOrgList)
      });
      this.getTreeselect();
    },
    methods: {
      // 筛选节点
      filterNode(value, data) {
        if (!value) return true;
        return data.label.indexOf(value) !== -1;
      },
      // 节点单击事件
      handleNodeClick(data) {
        this.orgnizationId = data.id;
        if ("4" == data.orgFunctionOpt) {
          this.addDisabled = true;
        }else{
          this.addDisabled = false;
        }
        this.orgnizationName = data.label;
        this.queryParams.classRoomId = data.id
        // this.deptData = data;
        this.currentLabName = data.label;
        this.getList();
      },
      /** 查询部门下拉树结构 */
      getTreeselect() {
        treeselect().then(response => {
          this.deptOptions = response.data;
        });
      },
      getTreeNodeLabel(deptId) {
        return getTreeLabel(this.deptOptions, deptId);
      },
      /** 查询实验室是否可预约列表 */
      getList() {
        if(!this.addDisabled){
          this.loading = false;
          return;
        }
        this.loading = true;
        listLabReservation(this.queryParams).then(response => {
          this.labReservationList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      },
      // 单位字典翻译
      flagOptFormat(row, column) {
        return this.selectDictLabel(this.holidayFlagOptions, row.holidayFlag);
      },
      // 第一节字典翻译
      sectionOneFormat(row, column) {
        return this.selectDictLabel(this.sectionOneOptions, row.sectionOne);

      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          dayId: null,
          holidayDate: null,
          holidayName: null,
          holidayFlag: null,
          sectionOne: null,
          sectionTwo: null,
          sectionThree: null,
          sectionFour: null,
          sectionFive: null,
          sectionSix: null,
          sectionSeven: null,
          sectionEight: null,
          sectionNine: null,
          sectionTen: null,
          sectionEleven: null,
          sectionTwelve: null,
          remark: null,
          createBy: null,
          createTime: null
        };
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.dayId)
        this.single = selection.length !== 1
        this.multiple = !selection.length
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset();
        this.open = true;
        this.title = "添加实验室是否可预约信息";
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset();
        const dayId = row.dayId || this.ids
        getLabReservation(dayId).then(response => {
          this.form = response.data;
          this.open = true;
          this.title = "修改实验室是否可预约";
        });
      },
      /** 提交按钮 */
      submitForm() {
        this.$refs["form"].validate(valid => {
          if (valid) {
            if (this.form.dayId != null) {
              updateLabReservation(this.form).then(response => {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              });
            } else {
              this.form.classRoomId = this.orgnizationId;
              addLabReservation(this.form).then(response => {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              });
            }
          }
        });
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const dayIds = row.dayId || this.ids;
        this.$confirm('是否确认删除实验室是否可预约编号为"' + dayIds + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function () {
          return delLabReservation(dayIds);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
      },
      /** 导出按钮操作 */
      handleExport() {
        const queryParams = this.queryParams;
        this.$confirm('是否确认导出所有实验室是否可预约数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function () {
          return exportLabReservation(queryParams);
        }).then(response => {
          this.download(response.msg);
        })
      },

      // 工作日变更改变所对应的值
      holidayFlagChange(value) {
        if (1 == value) {
          // this.form.holidayName=0,
          //  this.form.holidayFlag=0,
          this.form.sectionOne = 0,
            this.form.sectionTwo = 0,
            this.form.sectionThree = 0,
            this.form.sectionFour = 0,
            this.form.sectionFive = 0,
            this.form.sectionSix = 0,
            this.form.sectionSeven = 0,
            this.form.sectionEight = 0,
            this.form.sectionNine = 0,
            this.form.sectionTen = 0,
            this.form.sectionEleven = 0,
            this.form.sectionTwelve = 0
        } else {
          this.form.sectionOne = 1,
            this.form.sectionTwo = 1,
            this.form.sectionThree = 1,
            this.form.sectionFour = 1,
            this.form.sectionFive = 1,
            this.form.sectionSix = 1,
            this.form.sectionSeven = 1,
            this.form.sectionEight = 1,
            this.form.sectionNine = 1,
            this.form.sectionTen = 1,
            this.form.sectionEleven = 1,
            this.form.sectionTwelve = 1
        }
      },
      classRoomIdFormatter(value) {
        let datas = this.labOrgList
        for (let key in datas) {
          if (datas[key].orgId == value.classRoomId) {
            return datas[key].orgName;
          }
        }
        return "";
      },
      isPermit(permitKey) {
        if (this.$store.state.user.name == 'admin') {
          return true;
        }
        let permissions = this.$store.getters.permissions;
        return permissions.includes(permitKey);
      },
    }
  };
</script>
<style type="text/css" scoped>
  .input_w {
    width: 180px;
  }

  .back {
    background: linear-gradient(225deg, #9185FF 0%, #6392FF 100%);
    font-size: 10px;
    width: 20px;
    height: 20px;
    line-height: 20px;
    border-radius: 15px;
    color: #fff;
    position: absolute;
    left: 30px;
    z-index: 999
  }

  .checkmark {
    width: 20px;
    height: 20px;
    border-radius: 10px;
    background: #009933;
    display: inline-block;
    position: relative;
  }

  .checkmark:before,
  .checkmark::after {
    content: "";
    height: 14px;
    width: 3px;
    border-radius: 5px;
    display: block;
    background: white;
    position: absolute;
    top: 3px;
    /*40-6=34*/
    left: 10px;
    /*make the two rects in the middle of the cycle */
    transform: rotate(45deg);
    -ms-transform: rotate(45deg);
  }

  .checkmark::before {
    height: 7px;
    transform: rotate(-45deg);
    -ms-transform: rotate(-45deg);
    position: absolute;
    top: 9px;
    /*40-18=12  */
    left: 4px;
  }

  .fork {
    width: 20px;
    height: 20px;
    border-radius: 20px;
    background: red;
    display: inline-block;
    position: relative;
  }

  .g-right {
    display: inline-block;
    width: 15px;
    height: 2px;
    background: #fff;
    line-height: 0;
    font-size: 0;
    vertical-align: middle;
    -webkit-transform: rotate(45deg);
    position: absolute;
    top: 8px;
    left: 3px;
  }

  .g-right:after {
    content: "/";
    display: block;
    width: 15px;
    height: 2px;
    background: #fff;
    -webkit-transform: rotate(-90deg);
  }
</style>
